/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.commons;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.swing.JTable;
import org.balisunrise.application.Application;
import org.balisunrise.common.GridModel;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractVisualEntity;
import org.balisunrise.common.listeners.GridColumnListener;
import org.balisunrise.common.listeners.GridSelectionListener;
import org.balisunrise.swing.components.table.ListSelectionListenerImpl;
import org.balisunrise.swing.components.table.ColumnSelectionListenerImpl;
import org.balisunrise.swing.components.table.TableEnterAction;
import org.balisunrise.swing.components.table.TableFocusListener;

/**
 *
 * @author Glauco
 */
public class Grid extends AbstractVisualEntity
        implements org.balisunrise.common.Grid<JTable>{

    private GridModel gridModel;
    private List<GridSelectionListener> selectionListeners;
    private List<GridColumnListener> columnListeners;
    private JTable visualRepresentation;

    private ListSelectionListenerImpl tableSelectionListener;
    private ColumnSelectionListenerImpl columnSelectionListener;
    private TableEnterAction actions;
    private TableFocusListener focusListener;

    public Grid() {
        
        columnListeners = new ArrayList<GridColumnListener>();
        getColumnSelectionListener().setListeners(columnListeners);

        selectionListeners = new ArrayList<GridSelectionListener>();
        getTableSelectionListener().setGridSelectionListeners(selectionListeners);

        actions = new TableEnterAction();
        focusListener = new TableFocusListener();
    }

    public void setGridModel(GridModel gridModel) {
        this.gridModel = gridModel;
    }

    public void setVisualRepresentation(JTable visualRepresentation) {
        getTableSelectionListener().setTable(visualRepresentation);
        getColumnSelectionListener().setTable(visualRepresentation);
        actions.setTable(visualRepresentation);
        focusListener.setTable(visualRepresentation);
        this.visualRepresentation = visualRepresentation;
    }

    private ListSelectionListenerImpl getTableSelectionListener() {
        if (tableSelectionListener == null)
            tableSelectionListener = new ListSelectionListenerImpl(this);
        return tableSelectionListener;
    }

    private ColumnSelectionListenerImpl getColumnSelectionListener(){
        if(columnSelectionListener == null)
            columnSelectionListener = new ColumnSelectionListenerImpl(this);
        return columnSelectionListener;
    }

    @Override
    public GridModel getGridModel() {
        return gridModel;
    }

    @Override
    public void addSelectionListener(GridSelectionListener listener) {
        selectionListeners.add(listener);
    }

    @Override
    public boolean removeSelectionListener(GridSelectionListener listener) {
        try{
            return selectionListeners.remove(listener);
        }catch(Exception ex){
            Application.get().getLogger().log(Level.SEVERE, ex.getMessage(),
                    ex);
        }
        return false;
    }

    @Override
    public GridSelectionListener[] getSelectionListeners() {
        return selectionListeners.toArray(new GridSelectionListener[]{});
    }

    @Override
    public JTable getVisualRepresentation() {
        return visualRepresentation;
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará GRID.
     * @return VisualEntityType.GRID
     */
    @Override
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.GRID;
    }

    @Override
    public String getName() {
        return getVisualRepresentation().getName();
    }

    @Override
    public void addColumnListener(GridColumnListener gl) {
        columnListeners.add(gl);
    }

    @Override
    public boolean removeColumnListener(GridColumnListener gl) {
        return columnListeners.remove(gl);
    }

    @Override
    public GridColumnListener[] getColumnListeners() {
        return columnListeners.toArray(new GridColumnListener[]{});
    }
}
